package com.atguigu.gulimall.search.thread;

import rx.Completable;

import java.util.concurrent.*;
import java.util.function.Supplier;

//测试 CompletableFuture
public class MyCompletableFutureTest {

    //创建线程池
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主方法开始");

        //将一个Runnable交给指定的线程池executor执行。
//        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果" + i);
//        }, executor);

        /**
         * whenComplete感知
         */
//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果" + i);
//            return i;
//        }, executor).whenComplete((result,exeption)->{
//            //等待线程执行完毕，再执行
//            System.out.println("线程的结果："+result+";出现的异常："+exeption);
//        }).exceptionally((throwable)->{
//            //如果有异常，可以返回一个默认值
//            return 10;
//        });

        /**
         * handle处理
         */
//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 0;
//            System.out.println("运行结果" + i);
//            return i;
//        }, executor).handle((result,throwable)->{
//            //有异常一定没有结果
//            if (result!=null){
//                System.out.println("正常执行："+result);
//                return result;
//            }
//            if (throwable!=null){
//                System.out.println("发生了异常："+throwable);
//                return 0;
//            }
//            return 0;
//        });

        /**
         * 线程串行化
         */
//        .thenRunAsync(()->{
//            System.out.println("第二线程"+Thread.currentThread().getId());
//        })
//        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果" + i);
//            return i;
//        }, executor).thenApplyAsync((result)->{
//            System.out.println("上个任务的结果"+result);
//            return 10;
//        },executor);

        /**
         * 任务组合
         */
        CompletableFuture<Integer> completableFuture1 = CompletableFuture.supplyAsync(()->{
            System.out.println("当前线程" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果" + i);
            return i;
        }, executor);

        System.out.println(completableFuture1.get());



        System.out.println("主方法结束");

    }



}
